/**
 * 部门业务
 * Created by benben on 14-9-19.
 */
var Condition = require('../../utils/mongodb/Condition');
var Conditions = require('../../utils/mongodb/Conditions');
var Query = require('../../utils/mongodb/Query');
var Page = require('../../utils/page/Page');
var Department = require('../../modules/system/department');

var DepartmentService = function(){
    //模块
    this.model = Department;

};

/**
 * 保存
 * @param department
 * @param callback
 */
DepartmentService.prototype.save = function(department,callback){
    department.save(function(err) {
        callback(err);
    });
};

/**
 * 根据ID查询
 * @param id
 * @param callback
 */
DepartmentService.prototype.getById = function(id,callback){
    this.model.findOne({ _id: id}, function(err, department) {
        if (err) {
            callback(err);
        }
        var page = new Page({
            totalCount : 1,
            data : department
        });
        callback(null,page);
    });
};

/**
 * 更新
 * @param id
 * @param department
 * @param callback
 */
DepartmentService.prototype.updateById = function(id,department,callback){
    this.model.findOneAndUpdate({ _id: id }, department, function (err){
        callback(err);
    });
};

/**
 * 分页查询
 * @param query
 * @param callback
 */
DepartmentService.prototype.findPage = function(query,callback){
    var me = this;

    var conditions = me.createConditions(query.conditionObj);

    Query.findPageWithModel(me.model,conditions,query.start,query.limit,function(err,page){
        callback(err,page);
    });
};

/**
 * 根据请求创建查询条件
 * @param department
 * @returns {Array}
 */
DepartmentService.prototype.createConditions = function(department){
    var conditions = new Conditions({
        type : 'and',
        items : []
    });

    if(department.departmentName){
        var condition = new Condition({
            type : 'like',
            name : 'departmentName',
            value :  department.departmentName
        });
        conditions.items.push(condition);
    }

    if(department.departmentNo){
        var condition = new Condition({
            type : 'like',
            name : 'departmentNo',
            value :  department.departmentNo
        });
        conditions.items.push(condition);
    }

    return conditions;
};


/**
 * 树的形式获取部门
 * @param callback
 */
DepartmentService.prototype.getTreeJSON = function (callback) {
    var me = this;
    var Department = me.model;
    //总公司
    Department.where('status').equals(1)
        .select('_id departmentName type upDepartment')
        .exec(function(err,docs){
            console.log(docs);
            var datas = [];
            docs.forEach(function(doc){
                if(doc.type == 0){
                    var children = me.getChildrenDepartment(doc,docs);
                    if (children && children.length>0) {
                        doc.set('children',children);
                    }else{
                        doc.set('leaf',true,Boolean);
                    }
                    datas.push(doc);
                }
            });

            callback(datas);
        });
};

/**
 * 树的形式获取子部门
 * @param docs
 * @returns {Array}
 */
DepartmentService.prototype.getChildrenDepartment = function(father,docs) {
    var me = this;
    var datas = [];
    docs.forEach(function(doc){
        if(doc.upDepartment._id == father._id){
            var children = me.getChildrenDepartment(doc,docs);
            if (children && children.length>0) {
                doc.set('children',children);
            }else{
                doc.set('leaf',true,Boolean);
            }
            datas.push(doc);
        }
    });

    return datas;
};

/**
 * 根据id停用部门及其子部门
 * @param id
 * @param callback
 */
DepartmentService.prototype.stopById = function(id,callback){
    var Department = this.model;
    this.getNotStopChildrenCountById(id,function(count){
        if(count > 0){
            callback('还有子部门没有停用，请先停用子部门');
        } else{
            Department.findByIdAndUpdate(id, {$set: {status:2}}, function(err,doc){
                if(err){
                    callback(err);
                }else{
                    callback(null);
                }
            });
        }
    });

};

/**
 * 根据id获取没有停用的子部门数量
 * @param id
 * @param callback
 */
DepartmentService.prototype.getNotStopChildrenCountById = function(id,callback){
    this.model.where('upDepartment._id').equals(id)
        .where('status').equals(1)
        .count(function (err, count) {
            callback(count);
            console.log('isNotStopChildrenCount %d ', count);
        })
};

module.exports = DepartmentService;